﻿using System.Collections.Generic;

namespace ExtensionMethodExtravaganza
{
	public static class StringExtensions
	{
		/// <summary>
		/// Returns the default value if <paramref name="s"/> is null or empty.
		/// </summary>
		public static string Default(this string s, string defaultValue)
		{
			if (string.IsNullOrEmpty(s))
			{
				return defaultValue;
			}
			else
			{
				return s;
			}
		}

		/// <summary>
		/// Returns the default value if <paramref name="s"/> is null.
		/// </summary>
		public static string Default(this string s, string defaultValue, bool onlyWhenNull)
		{
			if (onlyWhenNull)
			{
				if (s == null)
				{
					return defaultValue;
				}
				else
				{
					return s;
				}
			}
			else
			{
				return Default(s, defaultValue);
			}
		}

		/// <summary>
		/// Creates list of non-null and empty values from the object passed to <paramref name="strings"/>.
		/// </summary>
		public static IEnumerable<string> RemoveEmptyElements(this IEnumerable<string> strings)
		{
			foreach (var s in strings)
			{
				if (!string.IsNullOrEmpty(s))
				{
					yield return s;
				}
			}
		}

		/// <summary>
		/// Same as String.Format, but with less typing.
		/// </summary>
		public static string Fmt(this string format, params object[] args)
		{
			return string.Format(format, args);
		}
	}
}
